import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _class;

import React from 'react';
import { Table, Icon } from 'antd';
import { Bind } from 'lodash-decorators';
import intl from 'utils/intl';
import { enableRender, TagRender, operatorRender } from 'utils/renderer';
import { tableScrollWidth } from 'utils/utils';
import Icons from 'components/Icons';
import { Button as ButtonPermission } from 'components/Permission';
var modelPrompt = 'ciam.tenantMenu.model.tenantMenu';
var commonPrompt = 'cpaas.common';
var menuIconStyle = {
  width: 14,
  height: 14,
  lineHeight: '14px'
};
var ListTable = (_dec = Bind(), _dec2 = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(ListTable, _React$Component);

  var _super = _createSuper(ListTable);

  function ListTable(props) {
    var _this;

    _classCallCheck(this, ListTable);

    _this = _super.call(this, props);
    _this.defaultTableRowKey = 'id';
    _this.state = {
      loading: false
    };
    return _this;
  }

  _createClass(ListTable, [{
    key: "onCell",
    value: function onCell() {
      return {
        style: {
          overflow: 'hidden',
          maxWidth: 220,
          textOverflow: 'ellipsis',
          whiteSpace: 'nowrap'
        },
        onClick: function onClick(e) {
          var target = e.target;

          if (target.style.whiteSpace === 'normal') {
            target.style.whiteSpace = 'nowrap';
          } else {
            target.style.whiteSpace = 'normal';
          }
        }
      };
    }
  }, {
    key: "handleEnable",
    value: function handleEnable(record, type) {
      var handleEnable = this.props.handleEnable;
      this.setState({
        loading: true
      });
      handleEnable(record, type);
      this.setState({
        loading: false
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          _this$props$menuDataS = _this$props.menuDataSource,
          menuDataSource = _this$props$menuDataS === void 0 ? [] : _this$props$menuDataS,
          path = _this$props.path,
          queryTreeListLoading = _this$props.queryTreeListLoading,
          _this$props$onCopy = _this$props.onCopy,
          onCopy = _this$props$onCopy === void 0 ? function (e) {
        return e;
      } : _this$props$onCopy,
          _this$props$onCreate = _this$props.onCreate,
          onCreate = _this$props$onCreate === void 0 ? function (e) {
        return e;
      } : _this$props$onCreate,
          _this$props$handleEdi = _this$props.handleEdit,
          handleEdit = _this$props$handleEdi === void 0 ? function (e) {
        return e;
      } : _this$props$handleEdi,
          _this$props$menuTypeL = _this$props.menuTypeList,
          menuTypeList = _this$props$menuTypeL === void 0 ? [] : _this$props$menuTypeL,
          _this$props$handleEdi2 = _this$props.handleEditPermissionSet,
          handleEditPermissionSet = _this$props$handleEdi2 === void 0 ? function (e) {
        return e;
      } : _this$props$handleEdi2,
          processing = _this$props.processing,
          processingEnableRow = _this$props.processingEnableRow,
          id = _this$props.id,
          _this$props$processin = _this$props.processing,
          enableLoading = _this$props$processin.enableLoading,
          disableLoading = _this$props$processin.disableLoading,
          _this$props$expandedR = _this$props.expandedRowKeys,
          expandedRowKeys = _this$props$expandedR === void 0 ? [] : _this$props$expandedR;
      var loading = this.state.loading;
      var filteredMenuTypeList = menuTypeList.filter(function (item) {
        return item.value !== 'ps';
      });
      var menuColumns = [{
        title: intl.get("".concat(modelPrompt, ".name")).d('目录/菜单'),
        dataIndex: 'name',
        fixed: 'left',
        width: 300
      }, {
        title: intl.get("".concat(modelPrompt, ".parentName")).d('上级目录'),
        width: 120,
        dataIndex: 'parentName'
      }, {
        title: intl.get("".concat(modelPrompt, ".quickIndex")).d('快速索引'),
        width: 120,
        dataIndex: 'quickIndex'
      }, {
        title: intl.get("".concat(modelPrompt, ".icon")).d('图标'),
        width: 60,
        dataIndex: 'icon',
        render: function render(code) {
          return /*#__PURE__*/React.createElement(Icons, {
            type: code,
            size: 14,
            style: menuIconStyle
          });
        }
      }, {
        title: intl.get("".concat(modelPrompt, ".code")).d('编码'),
        dataIndex: 'code',
        onCell: this.onCell.bind(this)
      }, {
        title: intl.get("".concat(modelPrompt, ".menuType")).d('类型'),
        dataIndex: 'type',
        width: 120,
        render: function render(value) {
          var statusList = filteredMenuTypeList.map(function (item) {
            return {
              status: item.value,
              // eslint-disable-next-line no-nested-ternary
              color: item.value === 'root' ? 'blue' : item.value === 'dir' ? 'green' : 'orange',
              text: item.meaning
            };
          });
          return TagRender(value, statusList);
        }
      }, {
        title: intl.get("".concat(modelPrompt, ".sort")).d('序号'),
        dataIndex: 'sort',
        width: 60
      }, {
        title: intl.get("".concat(modelPrompt, ".description")).d('描述'),
        dataIndex: 'description',
        width: 200,
        onCell: this.onCell.bind(this)
      }, {
        title: intl.get("".concat(commonPrompt, ".status")).d('状态'),
        dataIndex: 'enabledFlag',
        width: 120,
        fixed: 'right',
        render: enableRender
      }, {
        title: intl.get("".concat(commonPrompt, ".table.column.option")).d('操作'),
        width: 220,
        fixed: 'right',
        render: function render(text, record) {
          var operators = [];

          if (record.type === 'root' || record.type === 'dir') {
            operators.push({
              key: 'create',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.createList"),
                  type: 'button',
                  meaning: '菜单配置-列表新建'
                }],
                onClick: function onClick() {
                  return onCreate(record, true);
                }
              }, intl.get("".concat(commonPrompt, ".button.create")).d('新建')),
              len: 2,
              title: intl.get("".concat(commonPrompt, ".button.create")).d('新建')
            });
          }

          if (record.customFlag === 1 && record.tenantId.toString() === id) {
            operators.push({
              key: 'edit',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.editList"),
                  type: 'button',
                  meaning: '菜单配置-列表编辑'
                }],
                onClick: function onClick() {
                  return handleEdit(record, true);
                }
              }, intl.get("".concat(commonPrompt, ".button.edit")).d('编辑')),
              len: 2,
              title: intl.get("".concat(commonPrompt, ".button.edit")).d('编辑')
            });
          } else {
            operators.push({
              key: 'view',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.view"),
                  type: 'button',
                  meaning: '菜单配置-查看'
                }],
                onClick: function onClick() {
                  return handleEdit(record, false);
                }
              }, intl.get("".concat(commonPrompt, ".button.view")).d('查看')),
              len: 2,
              title: intl.get("".concat(commonPrompt, ".button.view")).d('查看')
            });
          }

          if ((processing.enableLoading || processing.disableLoading) && record.id === processingEnableRow) {
            operators.push({
              key: 'enable-disabled',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.loading"),
                  type: 'button',
                  meaning: '菜单配置-loading'
                }],
                key: "loading"
              }, /*#__PURE__*/React.createElement(Icon, {
                type: "loading"
              })),
              len: 4
            });
          } else if (record.enabledFlag === 1 && record.customFlag === 1 && record.tenantId.toString() === id) {
            operators.push({
              key: 'enable-disabled',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.disableList"),
                  type: 'button',
                  meaning: '菜单配置-列表禁用'
                }],
                onClick: function onClick() {
                  return _this2.handleEnable(record, 'disable');
                }
              }, intl.get("".concat(commonPrompt, ".status.disable")).d('禁用')),
              len: 2,
              title: intl.get("".concat(commonPrompt, ".status.disable")).d('禁用')
            });
          } else if (record.enabledFlag === 0 && record.customFlag === 1 && record.tenantId.toString() === id) {
            operators.push({
              key: 'enable-disabled',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.enableList"),
                  type: 'button',
                  meaning: '菜单配置-列表启用'
                }],
                onClick: function onClick() {
                  return _this2.handleEnable(record, 'enable');
                }
              }, intl.get("".concat(commonPrompt, ".status.enable")).d('启用')),
              len: 2,
              title: intl.get("".concat(commonPrompt, ".status.enable")).d('启用')
            });
          }

          if (['menu', 'inner-link', 'link'].includes(record.type) && record.customFlag === 1 && record.tenantId.toString() === id) {
            operators.push({
              key: 'permissionSet',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.permissionSet"),
                  type: 'button',
                  meaning: '租户菜单管理-列表权限集'
                }],
                onClick: function onClick() {
                  return handleEditPermissionSet(record);
                }
              }, intl.get('cpaas.common.button.permissionSet').d('权限集')),
              len: 3,
              title: intl.get('cpaas.common.button.permissionSet').d('权限集')
            });
          }

          operators.push(record.customFlag && record.tenantId.toString() === id && {
            key: 'copy',
            ele: /*#__PURE__*/React.createElement(ButtonPermission, {
              type: "text",
              permissionList: [{
                code: "".concat(path, ".button.copy"),
                type: 'button',
                meaning: '租户菜单管理-复制菜单'
              }],
              onClick: function onClick() {
                return onCopy(record);
              }
            }, intl.get('cpaas.common.button.copy').d('复制')),
            len: 2,
            title: intl.get('cpaas.common.button.copy').d('复制')
          });
          return operatorRender(operators);
        }
      }];
      var tableProps = {
        bordered: true,
        pagination: false,
        uncontrolled: true,
        columns: menuColumns,
        loading: queryTreeListLoading || enableLoading || disableLoading || loading,
        dataSource: menuDataSource,
        childrenColumnName: 'subMenus',
        rowKey: this.defaultTableRowKey,
        scroll: {
          x: tableScrollWidth(menuColumns)
        },
        expandedRowKeys: expandedRowKeys
      };
      return /*#__PURE__*/React.createElement(Table, tableProps);
    }
  }]);

  return ListTable;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "onCell", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "onCell"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleEnable", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handleEnable"), _class.prototype)), _class));
export { ListTable as default };